package com.itboye.textileexpres.top;

import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;
import com.itboye.textileexpres.top.ActivityCarmarSearch;

import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;

import com.itboye.textileexpes.beans.ForresterBeans;
import com.itboye.textileexpes.presenter.CategoryPresenter;
import com.itboye.textileexpes.presenter.MainCategoryPresenter;
import com.itboye.textileexpres.R;
import com.itboye.textileexpres.base.BaseActivity;
import com.itboye.textileexpres.base.ResultEntity;
import com.itboye.textileexpres.widget.PagerSlidingTabStrip;
import com.itboye.textillexepres.db.SearchDBManager;

public class ActivityGoodsSearch extends BaseActivity implements
		OnClickListener, Observer {
	ImageView back;
	TextView goods_num;
	String[] title;
	// = { "张丽坤", "赵丽颖", "马苏", "范冰冰", "李冰冰", "张柏芝", "马化腾" };
	ViewPager pager;
	PagerSlidingTabStrip tabs;
	ImageView carmar_img;
	MainCategoryPresenter presenter;
	ForresterBeans beans = new ForresterBeans();
	protected List<ForresterBeans> arrayListForm = new ArrayList<ForresterBeans>();
	protected List<String> CateID = new ArrayList<String>();
	SearchDBManager dbManager;

	@Override
	public int initView() {
		// TODO Auto-generated method stub
		return R.layout.activity_goods_search;
	}

	@Override
	public void onMyCreate() {
		// TODO Auto-generated method stub
		dbManager = new SearchDBManager(this);
		tabs.setFillViewport(false);
		presenter = new MainCategoryPresenter(this);
		presenter.getInfo(beans);

	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.back:
			finish();
			break;
		case R.id.carmar_img:
			Intent intent = new Intent(this, ActivityCarmarSearch.class);
			intent.putExtra("from", "other");
			startActivity(intent);
			break;
		default:
			break;
		}
	}

	public class MyAdapter extends FragmentStatePagerAdapter {
		String[] _titles;

		public MyAdapter(FragmentManager fm, String[] titles) {
			super(fm);
			_titles = titles;
		}

		@Override
		public CharSequence getPageTitle(int position) {
			return _titles[position];
		}

		@Override
		public int getCount() {
			return _titles.length;
		}

		@Override
		public Fragment getItem(int position) {
			Fragment bfrag2 = null;
			bfrag2 = new FragmentGoodsSearch(Integer.parseInt(CateID
					.get(position)), position);
			// bfrag2 = new FragmentGoodsSearch();
			return bfrag2;
		}

	}

	@Override
	public void update(Observable observable, Object data) {
		// TODO Auto-generated method stub
		ResultEntity entity = handlerError(data);
		if (entity != null) {
			if (entity.getEventType() == MainCategoryPresenter.category_success) {
				arrayListForm = (List<ForresterBeans>) entity.getData();
				title = new String[arrayListForm.size()];
				for (int i = 0; i < arrayListForm.size(); i++) {
					title[i] = arrayListForm.get(i).getName();
					CateID.add(arrayListForm.get(i).getId());
				}
				// goods_num.setText(""+arrayListForm.get(i))
				pager.setAdapter(new MyAdapter(getSupportFragmentManager(),
						title));
				tabs.setViewPager(pager);

			}
		}
	}
}
